package com.youwe.web.demo;

import java.net.URL;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import com.youwe.model.demo.Book;
import com.youwe.util.serializable.JSONUtils;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BookControllerTesting {
	
	private Logger Logger = LogManager.getLogger(BookControllerTesting.class);

	@LocalServerPort
	private int port;

	@Autowired
	private TestRestTemplate restTemplate;

	private URL base;

	private String context = "youwe-web";

	@Before
	public void setUp() {
		try {
			this.base = new URL("http://localhost:" + port + "/" + context + "/");
		}catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void getBook_Test() {
		Logger.info("Start---测试查询某一本书的详情");
		String accessUrl = base.toString() + "/book/one";		
		ResponseEntity<String> response = restTemplate.getForEntity(accessUrl, String.class);
		int status = response.getStatusCodeValue();
		assertThat(status).isEqualTo(200);
		Book book = JSONUtils.jsonToModel(response.getBody(), Book.class);
		assertThat(book.getName()).isEqualTo("《钢铁是怎么样炼成的》");
		Logger.info("End---测试查询某一本书的详情");
	}

}
